import 'package:enterprise_v6_app/app/components/common_selection/common_selection_controller.dart';
import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/common/choice_bean.dart';

/// 通用的列表选择控件
class CommonSelectionPage<T extends ChoiceBean> extends StatelessWidget {
  CommonSelectionPage({super.key, this.enableLoad, this.title});

  // 是否可以上拉加载
  final bool? enableLoad;

  // 标题
  final String? title;
  final controller = Get.put(CommonSelectionController<T>());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        titleText: title ?? '选择',
      ),
      body: GetBuilder(
          init: controller,
          builder: (_) {
            return Container(
              padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 10.w,bottom: 70.w),
              alignment: Alignment.topCenter,
              color: const Color(0xffF5F5F6),
              child: CustomEasyRefreshList<T>(
                isShowSearch: true,
                dataLoad: (pageNum, pageSize, {String? search}) async {
                  return controller.getDate(
                    pageNum: pageNum,
                    pageSize: pageSize,
                    enableLoad: enableLoad ?? false,
                    search: search,
                  );
                },
                enableLoad: enableLoad ?? false,
                itemBuilder: (T item, int index) {
                  return Container(
                      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 16.w),
                      decoration: item.getIsChecked()
                          ? BoxDecoration(
                              borderRadius: BorderRadius.circular(8.w),
                              gradient: const LinearGradient(
                                  //渐变位置
                                  begin: Alignment.centerLeft,
                                  end: Alignment.centerRight,
                                  //渐变颜色[始点颜色, 结束颜色]
                                  colors: [Color(0xff3B62FF), Color(0xff9FB2FF)]))
                          : BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
                      child: Row(
                        children: [
                          item.getIsChecked()
                              ? SvgPicture.asset(
                                  AppImages.icon_unit_select,
                                  width: 28.w,
                                  height: 28.w,
                                )
                              : SvgPicture.asset(
                                  AppImages.icon_unit_un_select,
                                  width: 28.w,
                                  height: 28.w,
                                ),
                          AppGaps.hGap12,
                          Expanded(
                            child: Text(
                              item.getShowName(),
                              style: TextStyle(
                                  color: item.getIsChecked() ? const Color(0xffffffff) : const Color(0xff262F38),
                                  height: 1.2,
                                  fontWeight: FontWeight.w400,
                                  fontSize: 16.sp),
                            ),
                          ),
                        ],
                      )).clickOnce(() {
                    controller.onTap(item);
                  });
                },
              ),
            );
          }),
      bottomSheet: Container(
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 5.w),
        decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: const Color(0xffBCBCC5).withOpacity(0.3), // 阴影颜色
              offset: const Offset(0, -5), // 阴影的偏移量，向上偏移，即上方阴影
              blurRadius: 10.0, // 阴影的模糊半径
              spreadRadius: 2.0, // 阴影的扩散半径
            ),
          ],
        ),
        child: Row(
          children: [
            Expanded(
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  backgroundColor: AppColors.blue_button,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(22.r),
                  ),
                ),
                onPressed: controller.complete,
                child: Text(
                  '确定',
                  style: TextStyle(
                    fontSize: 16.sp,
                    color: Colors.white,
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
